package com.dream.keigezhushou.Activity.acty.listen;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dream.keigezhushou.Activity.acty.listen.myview.AlertDialog;
import com.dream.keigezhushou.Activity.activity.BaseActivity;
import com.dream.keigezhushou.Activity.adapter.RecentPlayAdapter;
import com.dream.keigezhushou.Activity.pop.MoreWindow;
import com.dream.keigezhushou.Activity.pop.MoreWindowToMusicMessage;
import com.dream.keigezhushou.Activity.view.ItemRemoveRecylerView;
import com.dream.keigezhushou.Activity.view.OnItemClickListener;
import com.dream.keigezhushou.Activity.wigdt.DividerItemDeoration;
import com.dream.keigezhushou.R;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RecentPlayActivity extends BaseActivity implements View.OnClickListener{

    @BindView(R.id.iv_return)
    ImageView ivReturn;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rv_recent_play)
    ItemRemoveRecylerView rvRecentPlay;

    private ArrayList<String> recentList;
    private RecentPlayAdapter recentPlayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recent_play);
        ButterKnife.bind(this);

        initView();
        initData();
    }

    //初始化界面
    protected void initView(){

        tvTitle.setText("最近播放");

        recentList=new ArrayList<String>();
        for (int i = 1; i<21; i++){
            recentList.add(""+i);
        }


        recentPlayAdapter=new RecentPlayAdapter(this,recentList);
        rvRecentPlay.setLayoutManager(new LinearLayoutManager(this));
        rvRecentPlay.setAdapter(recentPlayAdapter);

        rvRecentPlay.addItemDecoration(new DividerItemDeoration(this, DividerItemDeoration.VERTICAL_LIST));
        /**Adapter中声明了more的ID所以要用Adapter设置点击事件对more进行点击
         * 但此处的RecycleView进行重写了 所以RecycleView中的Item点击需要在重新声明的ItemRecycleView中定义
         * */
        recentPlayAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

            }

            @Override
            public void onDeleteClick(int mPosition) {

            }

            @Override
            public void onItemMoreClick(int position) {
                final MoreWindow moreWindow = new MoreWindow(View.inflate(RecentPlayActivity.this, R.layout.golde30s_pupwindow, null), RecentPlayActivity.this, RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT);
                moreWindow.showPopAtLocation(rvRecentPlay, Gravity.BOTTOM);
                moreWindow.setItemListener(new MoreWindow.ItemClickListener() {
                    @Override
                    public void onClick() {
//                        Toast.makeText(HotRecommendActivity.this, "好样", Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onSingDetail() {


                        WindowManager wm = (WindowManager) RecentPlayActivity.this
                                .getSystemService(Context.WINDOW_SERVICE);

                        int width = wm.getDefaultDisplay().getWidth();
                        int height = wm.getDefaultDisplay().getHeight();


                        MoreWindowToMusicMessage moreWindowToMusicMessage = new MoreWindowToMusicMessage(View.inflate(RecentPlayActivity.this, R.layout.pop_musicmessage, null), RecentPlayActivity.this, width, ViewGroup.LayoutParams.WRAP_CONTENT);
//                       moreWindow.setBackColorState(0);
                        moreWindowToMusicMessage.showPopAtLocation(rvRecentPlay , Gravity.BOTTOM);
                        moreWindow.backGroundAlpha(0.4f);


                    }
                });

            }
        });



        rvRecentPlay.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(RecentPlayActivity.this, "** " + recentList.get(position-1) + " **", Toast.LENGTH_SHORT).show();



            }

            @Override
            public void onDeleteClick(final int mPosition) {
                if(mPosition>0) {
                    DisplayMetrics metric = new DisplayMetrics();
                    getWindowManager().getDefaultDisplay().getMetrics(metric);
                    int width = (int) (metric.widthPixels * 0.6);
//                    int height = (int) (metric.heightPixels);
                    AlertDialog myalerdialog = new AlertDialog(View.inflate(RecentPlayActivity.this, R.layout.alertdialog, null), RecentPlayActivity.this, width, ViewGroup.LayoutParams.WRAP_CONTENT);
                    myalerdialog.showPopAtLocation(rvRecentPlay, Gravity.CENTER_HORIZONTAL);

                    myalerdialog.setItemListener(new AlertDialog.ItemClickListener() {
                        @Override
                        public void onClick() {


                            recentPlayAdapter.removeItem(mPosition - 1);

                        }


                    });

                }

            }

            @Override
            public void onItemMoreClick(int position) {



            }

        });

        recentPlayAdapter.addHeaderView(View.inflate(this, R.layout.golde30s_collecte_headerview, null));
        recentPlayAdapter.addFootView(View.inflate(this, R.layout.golde30s_collecte_footview, null));
    }

    //初始化数据
    protected void initData(){

        ivReturn.setOnClickListener(this);
    }


    //点击事件的实现

    @Override
    public void onClick(View view) {

        switch (view.getId()){

            case R.id.iv_return:

                //返回
                finish();
                break;
        }
    }
}
